package com.macro.mall.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.mgb.mapper.BrandMapper;
import com.macro.mall.mgb.mapper.ProductMapper;
import com.macro.mall.mgb.model.Brand;
import com.macro.mall.mgb.model.Product;
import com.macro.mall.portal.dao.HomeDao;
import com.macro.mall.portal.domain.BrandDto;
import com.macro.mall.portal.domain.HomeProductDto;
import com.macro.mall.portal.domain.SimpleProductDTO;
import com.macro.mall.portal.service.PortalBrandService;
import com.macro.mall.portal.util.GenericConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wangwang
 * @version 1.0
 * @description 分页获取推荐品牌
 * @date 2025/7/27 20:35
 */
@Service
public class PortalBrandServiceImpl implements PortalBrandService {

    @Autowired
    private HomeDao homeDao;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<BrandDto> recommendList(Integer pageNum, Integer pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return GenericConverter.convertList(homeDao.getRecommendBrandList(offset, pageSize),BrandDto.class);
    }

    @Override
    public Brand detail(Long brandId) {
        return brandMapper.selectById(brandId);
    }

    @Override
    public CommonPage<SimpleProductDTO> productList(Long brandId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getBrandId,brandId).eq(Product::getDeleteStatus,0)
                .eq(Product::getPublishStatus,1);
        Page<Product> page = new Page<>(pageNum, pageSize);
        Page<Product> productPage = productMapper.selectPage(page, queryWrapper);
        List<SimpleProductDTO> productDTOList = GenericConverter.convertList(productPage.getRecords(), SimpleProductDTO.class);
        return CommonPage.restPage(productDTOList);
    }
}
